package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMysignupdetailBinding;
import com.sgb.kjwl.model.entity.enterprise.MyNeedQuoteDatailEntity;
import com.sgb.kjwl.viewmodel.enterprise.MySignUpDetailViewModel;

/**
 * 我的报价详情
 */
public class MySignUpDetailActivity extends AppActivity {

    private ActivityMysignupdetailBinding mBinding;
    private MySignUpDetailViewModel mViewModel;
    MyNeedQuoteDatailEntity entity;


    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(MySignUpDetailViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_mysignupdetail);
        mBinding.setLifecycleOwner(this);

        initClick();
    }

    @Override
    protected void init() {
        String xqid = getIntent().getStringExtra("xqid");
        String bjid = getIntent().getStringExtra("bjid");
        mViewModel.getDetails(xqid, bjid);
        mViewModel.dateDetail.observe(this, this::setDetail);
    }


    private void initClick() {
        mBinding.tvLooknotice.setOnClickListener(this);
        mBinding.imgLooknotice.setOnClickListener(this);
        mBinding.linNeeddetail.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.tv_looknotice:
            case R.id.img_looknotice:
                SignUpSuccessNoticeActivity.start(MySignUpDetailActivity.this, entity);
                break;

            case R.id.lin_needdetail:
                NewNeedDetailsActivity.startactivity(MySignUpDetailActivity.this,mViewModel.xqid);
                break;


        }
    }

    private void setDetail(MyNeedQuoteDatailEntity entity) {
        this.entity = entity;
        mBinding.setViewModel(entity);
        mBinding.nsvMyOfferDetail.setVisibility(View.VISIBLE);
        switch (entity.getRequirementTypes()) {
            case "1":
                mBinding.tvNeedtype.setText("商标需求");
                break;
            case "2":
                mBinding.tvNeedtype.setText("专利需求");
                break;
            case "3":
                mBinding.tvNeedtype.setText("版权需求");
                break;
            case "4":
                mBinding.tvNeedtype.setText("公司需求");
                break;
            case "5":
                mBinding.tvNeedtype.setText("服务需求");
                break;
            case "6":
                mBinding.tvNeedtype.setText("资产需求");
                break;
        }

        if (!TextUtils.isEmpty(entity.getAttachmentUrl()) && !entity.getAttachmentUrl().equals("{}")) {
            mBinding.linCode.setVisibility(View.VISIBLE);
            if (entity.getAttachmentUrl().indexOf("{") > -1) {

                JSONObject jsonObject = JSONObject.parseObject(entity.getAttachmentUrl());
                if (null != jsonObject.get("name")) {
                    String name = jsonObject.getString("name");
                    mBinding.tvName.setText(name);
                }
                if (null != jsonObject.get("size")) {
                    mBinding.tvSize.setText(jsonObject.getString("size"));
                }
                if (jsonObject.get("type") != null) {
                    String fileType = jsonObject.getString("type");

                    switch (fileType) {
                        case "application/zip":
                        case "application/rar":
                            Glide.with(this).load(R.mipmap.icon_zip).into(mBinding.image);
                            break;
                        case "image/png":
                        case "image/jpeg":
                            Glide.with(this).load(R.mipmap.icon_image).into(mBinding.image);
                            break;
                        case "application/pdf":
                            Glide.with(this).load(R.mipmap.icon_pdf2).into(mBinding.image);
                            break;
                    }
                }

            }
        } else {
            mBinding.linCode.setVisibility(View.GONE);
        }

        switch (entity.getStatus()) {
            case "1"://报价中
                mBinding.offerStatusIng.setVisibility(View.VISIBLE);
                mBinding.offerStatusSuccess.setVisibility(View.GONE);
                mBinding.offerStatusFail.setVisibility(View.GONE);
                break;
            case "2"://报价成功
                mBinding.offerStatusIng.setVisibility(View.GONE);
                mBinding.offerStatusSuccess.setVisibility(View.VISIBLE);
                mBinding.offerStatusFail.setVisibility(View.GONE);
                break;
            case "3"://报价失败
                mBinding.offerStatusIng.setVisibility(View.GONE);
                mBinding.offerStatusSuccess.setVisibility(View.GONE);
                mBinding.offerStatusFail.setVisibility(View.VISIBLE);
                break;
        }
    }

    public static void start(Context mContext, String xqid, String bjid) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, MySignUpDetailActivity.class);
        mIntent.putExtra("xqid", xqid);
        mIntent.putExtra("bjid", bjid);
        mContext.startActivity(mIntent);
    }


}
